package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.fl.system.api.ISysSequenceApiService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssSchFeeDService;
import cn.fl.ass.transform.AssParkingVOTransform;
import cn.fl.ass.transform.AssSchFeeDVOTransform;
import cn.fl.ass.vo.AssSchFeeDVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.transform.SysUlFilesRecVOTransform;
import cn.fl.system.vo.SysUlFilesRecVO;
import fl.ass.facade.api.IAssSchFeeDApi;
import fl.ass.facade.constant.AssFileConstant;
import fl.ass.facade.dto.AssParkingDTO;
import fl.ass.facade.dto.AssSchFeeDDTO;
import fl.ass.facade.dto.MessageDTO;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;



@Service
public class AssSchFeeDServiceImpl implements IAssSchFeeDService {
	
	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(IAssSchFeeDService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAssSchFeeDApi assSchFeeDApi ;
	/**
	 * sequenceApiService 生成主键id
	 */
	@Autowired
	private ISysSequenceApiService sysSequenceApiService;

	
	@Override
	public MessageDTO<String> saveAssSchFeeD(Map<String, Object> map, List<AssSchFeeDVO> assSchFeeVoList) throws BaseException {
		//保存上传文件
		
		List<AssSchFeeDDTO> assSchFeeDDTOList = new ArrayList<AssSchFeeDDTO>();
		for(AssSchFeeDVO assSchFeeDVO : assSchFeeVoList){
			List<SysUlFilesRecDTO> fileList = new ArrayList<SysUlFilesRecDTO>();
			
			if(CollectionUtils.isNotEmpty(assSchFeeDVO.getFileList())){
				List<SysUlFilesRecVO> fileVOList = assSchFeeDVO.getFileList();
				for (SysUlFilesRecVO sysUlFilesRecVO : fileVOList) {
					sysUlFilesRecVO.setTabNm("ASS_SCH_FEE_D");
					sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
					sysUlFilesRecVO.setOrgCd(AssFileConstant.ASS_PARKING_CFGORG);
					sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
					sysUlFilesRecVO.setCfgCd("ASS0015");
					sysUlFilesRecVO.setCfgDtlCd("10");//PrjPrdPaySchDConstant.PRJ_PRD_Pay_SCH_D_FILE_DETAIL_CODE
					fileList.add(SysUlFilesRecVOTransform.toDTO(sysUlFilesRecVO));
				}
			}
			AssSchFeeDDTO assSchFeeDDTO = new AssSchFeeDDTO();
			assSchFeeDDTO = AssSchFeeDVOTransform.toDTO(assSchFeeDVO);
			assSchFeeDDTO.setFileList(fileList);
			assSchFeeDDTOList.add(assSchFeeDDTO);
		}
		FlowStartParamsDTO flowStartParamsDTO = this.getFlowStartParams("ASS_SCH_FEE_D","023");//表名,流程分类
		MessageDTO<String> response  = assSchFeeDApi.saveAssSchFeeD(map,assSchFeeDDTOList,flowStartParamsDTO);
		return response;
	}


	@Override
	public Long getId() throws BaseException {
		Long id = (long) 0;
		try {
			id = sysSequenceApiService.getFun("FN_GEN_ASS_SCH_FEE_M_ID");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pager selectById(Pager page)throws BaseException {
		if(null == page)page = new Pager();
		 page = assSchFeeDApi.selectById(page);
		 
		List<AssSchFeeDDTO> list = (List<AssSchFeeDDTO>)page.getResultList();
		 page.setResultList(AssSchFeeDVOTransform.toVOList(list));
		 return page;
	}
	
	
	/******************************************************
     * 获取发起流程对象  -  已拖回追加费用项
     * @param businessType:业务类型(FlowConstant.FLOW_BUSINESS_TYP_RISK)
     * @param modelTyp:流程业务分类:FlowConstant.FLOW_MODEL_TYP_RISK
     * @return
     * @throws BaseException
     *****************************************************/
    private FlowStartParamsDTO getFlowStartParams(String businessType,String modelTyp)throws BaseException{
    	if(StringUtils.isEmpty(modelTyp)){
    		throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程业务分类不能为空");
    	}
        FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
        //流程定义KEY和流程定义ID不定义  流程发起的时候会根据模块类型查找唯一流程去发起
        flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
        flowStartParamsDTO.setWordsInTheTable("prj");
        flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
        flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
        flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
        flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);//优先级,可以在界面定义
        flowStartParamsDTO.setBusinessType(businessType);//TODO 确认用不用
        flowStartParamsDTO.setModelTyp(modelTyp);//需要和流程配置中的模块类型匹配
        return flowStartParamsDTO;
    }
    
    
    
    
}

